<?php
/**
 * 商家管理
 * User: lxq
 * Date: 2018/3/23
 * Time: 18:34
 */
class merchantControl extends  SystemControl{

    private $links = array(
        array('url'=>'act=merchant&op=index','lang'=>'nc_manage'),
        array('url'=>'act=merchant&op=add','lang'=>'nc_new'),
        //array('url'=>'act=member&op=merchant_edit','lang'=>'nc_edit')
    );
    public function __construct(){
        parent::__construct();
        Language::read('member');
    }

    //列表
    public function indexOp()
    {
        /** @var member_merchantModel $model */
        $model = Model('member_merchant');

        //查询条件
        $condition=array();
        if (!empty($_GET['search_shop_name'])){
            $condition['mc_shop_name'] = array('like','%'.$_GET['search_shop_name'].'%');
        }
        $time_from = strtotime($_GET['search_stime']);
        $time_to = strtotime($_GET['search_etime']);
        if ($time_from && $time_to) {
            $time_to = $time_to + 86400;
            $condition['mc_apply_time'] = array('between',array($time_from,$time_to));
        } elseif ($time_from) {
            $condition['mc_apply_time'] = array('egt',$time_from);
        } elseif ($time_to) {
            $time_to = $time_to + 86400;
            $condition['mc_apply_time'] = array('elt',$time_to);
        }
        $merchant_id = intval($this->admin_info['merchant_id']);
        if($merchant_id>0) {
            $condition['merchant_id'] = $merchant_id;
            Tpl::output('merchant_id',$merchant_id);
        }

        if (isset($_GET['search_status']) && is_numeric($_GET['search_status'])) {
            $condition['mc_status'] = intval($_GET['search_status']);
        }


        $list = $model->getMerchantListPage($condition, '*', 10);
        foreach ($list as $k => $v) {
            $list[$k]['mc_lic_img'] = UPLOAD.'product/'.$v['mc_lic_img'];
            $list[$k]['mc_idcard_img'] = UPLOAD.'product/'.$v['mc_idcard_img'];
            $list[$k]['mc_idcardb_img'] = UPLOAD.'product/'.$v['mc_idcardb_img'];
        }

        Tpl::output('search', $_GET);
        Tpl::output('merchant_list',$list);
        Tpl::output('page',$model->showpage());
        Tpl::output('state',$model->state);
        Tpl::output('bank_state',$model->bank_state);
        if($merchant_id==0){
            Tpl::output('top_link',$this->sublink($this->links,'index'));
        }
        Tpl::showpage('merchant.index');

    }

    /**
     *  添加商家
     */
    public function addOp()
    {
        $model = Model('member_merchant');
        $ticket_category = Model('ticket_category');
        if(chksubmit()){
            $param = array();
            $param['mc_shop_name'] = $_POST['mc_shop_name'];
            $param['mch_shortname'] = $_POST['mch_shortname'];
            $param['mc_phone'] = $_POST['mc_phone'];
            $param['mc_name'] = $_POST['mc_name'];
            $param['city'] = $_POST['city'];
            $param['address'] = $_POST['address'];
            $param['service_phone'] = $_POST['service_phone'];
            $param['id_card_no'] = $_POST['id_card_no'];
            $param['mc_cate']  =  $_POST['cate1'];
            $param['province_id'] = $_POST['province_id'];
            $param['city_id']  = $_POST['city_id'];
            $param['area_id']  = $_POST['area_id'];
            $param['mc_content'] = $_POST['content'];
            $param['mc_cate_name'] = Model('ticket_category')->getfby_tc_id($_POST['cate1'], 'tc_name');
            $param['mc_status'] = 1;
            $param['mc_apply_time'] = time();
            $param['pd_tariff'] = $_POST['pd_tariff'];
            //如果营业执照有重新上传的话
            if ($_FILES['mc_lic_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_lic_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_lic_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket'),0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果身份证正面照片有重新上传的话
            if ($_FILES['mc_idcard_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_idcard_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_idcard_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket'),0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果身份证反面照片有重新上传的话
            if ($_FILES['mc_idcardb_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_idcardb_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_idcardb_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket'),0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果封面图有重新上传的话
            if ($_FILES['mc_cover_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_cover_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_cover_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket'),0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            $res = $model->addMerchant($param);
            if($res){
                $this->log('添加商家成功'.'['.$res.']',1);
                showMessage('添加商家成功', 'index.php?act=merchant&op=index');
            }else{
                showMessage(L('nc_common_save_fail'));
            }

        }
        /* @var areaModel $area_model */
        $area_model = Model('area');
        $cate = $ticket_category->getCategoryList(array('tc_pid'=>0,'is_del'=>0));//获取酒店分类
        $provinces = $area_model->getAreaList(array('parent_id'=>1));
        Tpl::output('provinces', $provinces);
        Tpl::output('top_link',$this->sublink($this->links,'add'));
        Tpl::output('cate',$cate);
        Tpl::showpage('merchant_add');
    }

    //编辑
    public function editOp()
    {
        $model = Model('member_merchant');
        $merchant_id = intval($this->admin_info['merchant_id'])>0?intval($this->admin_info['merchant_id']):intval($_GET['id']);
        $merchant_info = $model->getMerchantInfo(array('merchant_id'=>$merchant_id));
        $area_model = Model('area');
        if(chksubmit()){
            $param = array();
            $param['mc_shop_name'] = $_POST['mc_shop_name'];
            $param['mch_shortname'] = $_POST['mch_shortname'];
            $param['mc_name'] = $_POST['mc_name'];
            $param['mc_phone'] = $_POST['mc_phone'];
            $param['city'] = $_POST['city'];
            $param['address'] = $_POST['address'];
            $param['service_phone'] = $_POST['service_phone'];
            $param['id_card_no'] = $_POST['id_card_no'];
            $param['mc_cate']  =  $_POST['cate1'];
            $param['province_id'] = $_POST['province_id'];
            $param['city_id']  = $_POST['city_id'];
            $param['area_id']  = $_POST['area_id'];
            $param['mc_content'] = $_POST['content'];
            $param['mc_cate_name'] = Model('ticket_category')->getfby_tc_id($_POST['cate1'], 'tc_name');
            $param['pd_tariff'] = $_POST['pd_tariff'];
            //如果营业执照有重新上传的话
            if ($_FILES['mc_lic_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_lic_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_lic_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket').'['.$merchant_id.']',0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果身份证正面照片有重新上传的话
            if ($_FILES['mc_idcard_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_idcard_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_idcard_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket').'['.$merchant_id.']',0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果身份证反面照片有重新上传的话
            if ($_FILES['mc_idcardb_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_idcardb_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_idcardb_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket').'['.$merchant_id.']',0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }
            //如果封面图有重新上传的话
            if ($_FILES['mc_cover_img']['tmp_name']) {
                $upload = new UploadFile();
                $upload->set('max_size', 2048);
                $upload->set('default_dir', 'product/' . $upload->getSysSetPath());
                //封面图片
                $result = $upload->upfile('mc_cover_img');
                if($result){
                    //得到图片上传后的路径
                    $param['mc_cover_img'] = $upload->getSysSetPath() . $upload->file_name;
                } else {
                    //图片上传失败
                    $this->log(L('nc_edit,ticket_index_ticket').'['.$merchant_id.']',0);
                    showMessage(L('nc_common_save_fail').$upload->error);
                }
            }


            $res = $model->updateMerchant(array('merchant_id'=>$merchant_id),$param);//信息写入数据库

            if($res){
                $this->log('编辑商家成功'.'['.$merchant_id.']',1);
                showMessage('编辑商家成功', 'index.php?act=merchant&op=index');
            }else{
                showMessage(L('nc_common_save_fail'));
            }

        }

        $merchant_info['mc_lic_img'] = UPLOAD . 'product/' .$merchant_info['mc_lic_img'];//商家营业执照
        $merchant_info['mc_idcard_img'] = UPLOAD . 'product/' .$merchant_info['mc_idcard_img'];//身份证正面照片
        $merchant_info['mc_idcardb_img'] = UPLOAD.'product/'.$merchant_info['mc_idcardb_img'];//反面照
        $merchant_info['mc_cover_img']  = UPLOAD.'product/'.$merchant_info['mc_cover_img'];//封面图
        $ticket_category = Model('ticket_category');
        $cate = $ticket_category->getCategoryList(array('tc_pid'=>0,'is_del'=>0));//获取酒店分类
        //获取已经选中市、县
        $citys = $area_model->getAreaList(array('parent_id'=>$merchant_info['province_id']));
        $areas = $area_model->getAreaList(array('parent_id'=>$merchant_info['city_id']));
        $provinces = $area_model->getAreaList(array('parent_id'=>1));
        Tpl::output('provinces', $provinces);
        Tpl::output('citys', $citys);
        Tpl::output('areas', $areas);
        Tpl::output('cate',$cate);
        Tpl::output('merchant_info',$merchant_info);
        Tpl::output('top_link', $this->sublink($this->links, 'merchant_edit'));
        Tpl::showpage('merchant_edit');

    }

    //商家状态
    public function merchant_stateOp()
    {
        $mid = intval($_POST['mid']);
        $type = intval($_POST['type']);
        if($type==1){
            if($mid>0){
                //所有商品进行下架处理
                $model = Model('ticket');
                $res = $model->updateTicket(array('merchant_id'=>$mid),array('tk_status'=>0));
                $re = Model('member_merchant')->updateMerchant(array('merchant_id'=>$mid),array('is_disable'=>1));
                $re && exit(json_encode(array('errcode'=>0)));
            }
        }elseif($type==2){
            //启用
            if($mid>0){
                $re = Model('member_merchant')->updateMerchant(array('merchant_id'=>$mid),array('is_disable'=>0));
                $re && exit(json_encode(array('errcode'=>0)));
            }
        }

        exit(json_encode(array('errcode'=>4000)));
    }

    /**
     * 审核
     */
    public function merchant_checkOp()
    {
        /** @var member_merchantModel $model */
        $model = Model('member_merchant');
        /** @var memberModel $merber_model */
        $merber_model = Model('member');

        $mid = intval($_POST['mid']);
        $state = intval($_POST['state']);
        if ($state === 1) {
            $merchant_info = $model->getMerchantInfo(array('merchant_id'=>$mid));
            $res = $model->updateMerchant(array('merchant_id'=>$mid), array('mc_status'=>1));
            if ($res) {
                $res = $merber_model->updateMember(array('member_id'=>$merchant_info['member_id']), array('is_merchant'=>1,'merchant_id'=>$mid));
                $res && exit(json_encode(array('errcode'=>0)));
            }
        } elseif ($state === 2) {
            $res = $model->updateMerchant(array('merchant_id'=>$mid), array('mc_status'=>2));
            $res && exit(json_encode(array('errcode'=>0)));
        }
        exit(json_encode(array('errcode'=>4000)));
    }

    /**
     * 检查该商家的输入的手机号是否已经存在
     */
    public function check_mobileOp()
    {
        $model = Model('member_merchant');
        $mc_phone = $model->getMerchantInfo(array('mc_phone'=>$_GET['mc_phone']),'mc_phone,merchant_id');
        if($_GET['merchant']>0){
            if(empty($mc_phone) || $mc_phone['merchant_id'] ==$_GET['merchant'] ){
                exit('true');
            }else{
                exit('false');
            }
        }else{
            if(empty($mc_phone)){
                exit('true');
            }else{
                exit('false');
            }
        }

    }


}